//
//  SCAudioVisualViewController.m
//  OurMoon
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "SCAudioVisualViewController.h"
#import "NightHeader.h"
#import "HTTPMethod.h"
#import "SCAudioVisualTableView.h"
#import "SCHeaderCollectionView.h"
#import "VideoListModel.h"
#import "VideoSidListModel.h"
#import "SCVideoPlayViewController.h"
#import "SCHeaderViewController.h"
#import "SCRadioModel.h"
#import "SCRadioSectionModel.h"

@interface SCAudioVisualViewController ()<UIScrollViewDelegate, HeaderViewControllerByVideoDelegate, VideoPlayViewControllerDelegate>
@property(nonatomic, strong)UISegmentedControl *segmentedControl;
@property(nonatomic, strong)UIScrollView *bottomScrollView; //底部滚动scrollw
@property(nonatomic, strong)SCAudioVisualTableView *videoTableView; //视频
@property(nonatomic, strong)SCHeaderCollectionView *videoHeaderCollectionView;//视频headerView
@property(nonatomic, strong)SCAudioVisualTableView *radioTableView; //电台
@property(nonatomic, strong)SCHeaderCollectionView *radioHeaderCollectionView;//电台headerView
@property(nonatomic, strong)NSMutableArray *dataVideoListArray;
@property(nonatomic, strong)NSMutableArray *dataVideoSidListArray;
@property(nonatomic, strong)NSMutableArray *dataRadioListArray;
@property(nonatomic, strong)NSMutableArray *dataRadioSectionArray;
@end

@implementation SCAudioVisualViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *nameArray = @[@"视频", @"电台"];
    _segmentedControl = [[UISegmentedControl alloc] initWithItems:nameArray];
    _segmentedControl.frame = CGRectMake(100, 0, 200, 30);
    //修改边框颜色
    _segmentedControl.tintColor = [UIColor whiteColor];
    //点击是否显示选中状态
    _segmentedControl.momentary = NO;
    //修改字体颜色
    //  [_segmentControl setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], UITextAttributeTextColor, [UIFont systemFontOfSize:20], UITextAttributeFont, nil] forState:UIControlStateNormal];
    _segmentedControl.layer.masksToBounds = YES;
    _segmentedControl.layer.cornerRadius = 10;
    self.navigationItem.titleView = _segmentedControl;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"变" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction)];
}
- (void)rightBarButtonAction
{
    [ThemeManage shareThemeManage].isNight = ![ThemeManage shareThemeManage].isNight;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:nil];
    [[NSUserDefaults standardUserDefaults] setBool:[ThemeManage shareThemeManage].isNight forKey:@"night"];
}
- (void)loadView
{
    [super loadView];
    [self getVideoData];
    [self getRadioData];
    //初始化底部scrollw
    _bottomScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 554)];
    _bottomScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 554);
    _bottomScrollView.contentOffset = CGPointMake(0, 0);
    _bottomScrollView.showsHorizontalScrollIndicator = NO; //横向
    _bottomScrollView.pagingEnabled = YES;
    _bottomScrollView.delegate = self;
    [self.view addSubview:_bottomScrollView];
    
    //初始化视频
    _videoTableView = [[SCAudioVisualTableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 554) WithType:SCTableViewByVideo];
    _videoTableView.delegate = self;
    [_bottomScrollView addSubview:_videoTableView];
    //初始化视频headerView
    _videoTableView.tableView.tableHeaderView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 97)];

    //初始化HeaderCollectionView
   _videoHeaderCollectionView = [[SCHeaderCollectionView alloc] initWithFrame:_videoTableView.tableView.tableHeaderView.frame WithType:SCCollectionViewFlowLayoutByVideo];
   _videoHeaderCollectionView.delegate = self;
    //为视频headerView赋值
   _videoTableView.tableView.tableHeaderView = _videoHeaderCollectionView;

    //初始化电台
    _radioTableView = [[SCAudioVisualTableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, 554) WithType:SCTableViewByRadio];
    [_bottomScrollView addSubview:_radioTableView];
    //初始化电台headerView
    _radioTableView.tableView.tableHeaderView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
    //初始化HeaderCollectionView
    _radioHeaderCollectionView = [[SCHeaderCollectionView alloc] initWithFrame:_radioTableView.tableView.tableHeaderView.frame WithType:SCCollectionViewFlowLayoutByRadio];
    //为视频headerView赋值
    _radioTableView.tableView.tableHeaderView = _radioHeaderCollectionView;
}
#pragma mark - 获取视频数据
- (void)getVideoData
{
    [HTTPMethod getDataByString:@"http://c.m.163.com/nc/video/home/0-10.html" BodyDic:nil WithDataBlock:^(id data) {
        self.dataVideoListArray = [NSMutableArray array];
        self.dataVideoSidListArray = [NSMutableArray array];
        for (NSDictionary *videoListdic in [data objectForKey:@"videoList"]) {
            VideoListModel *videoListModel = [[VideoListModel alloc] init];
            [videoListModel setValuesForKeysWithDictionary:videoListdic];
            [_dataVideoListArray addObject:videoListModel];
        }
        for (NSDictionary *videoSidListDic in [data objectForKey:@"videoSidList"]) {
            VideoSidListModel *videoSidListModel = [[VideoSidListModel alloc] init];
            [videoSidListModel setValuesForKeysWithDictionary:videoSidListDic];
            [_dataVideoSidListArray addObject:videoSidListModel];
        }
        _videoHeaderCollectionView.dataVideoSidListArray = _dataVideoSidListArray;
        _videoTableView.dataVideoListArray = _dataVideoListArray;
         _radioHeaderCollectionView.dataVideoListArray = [NSMutableArray arrayWithObjects:@"我的下载", @"最近播放", nil];
        [_videoTableView.tableView reloadData];
    }];
}

#pragma mark - 获取电台数据
- (void)getRadioData
{
    self.dataRadioListArray = [NSMutableArray array];
    self.dataRadioSectionArray = [NSMutableArray array];
    [HTTPMethod getDataByString:@"http://c.m.163.com/nc/topicset/android/radio/index.html" BodyDic:nil WithDataBlock:^(id data) {
        for (NSDictionary *dic in [data objectForKey:@"cList"]) {
            //
            SCRadioSectionModel *radioSectionModel = [[SCRadioSectionModel alloc] init];
            radioSectionModel.cid = [dic objectForKey:@"cid"];
            radioSectionModel.cname = [dic objectForKey:@"cname"];
            [_dataRadioListArray addObject:radioSectionModel];
            //
            NSMutableArray *array = [NSMutableArray array];
            SCRadioModel *radioModel = nil;
            for (NSDictionary *tListDic in [dic objectForKey:@"tList"]) {
                NSDictionary *radioDic = [NSDictionary dictionaryWithDictionary:[tListDic objectForKey:@"radio"]];
                radioModel = [[SCRadioModel alloc] init];
                [radioModel setValuesForKeysWithDictionary:radioDic];
                [array addObject:radioModel];
            }
            [_dataRadioListArray addObject:array];
            [_dataRadioListArray.lastObject addObject:radioModel];
        }
       // _radioTableView.dataRadioSectionArray = _dataRadioSectionArray;
        _radioTableView.dataRadioListArray = _dataRadioListArray;
    }];
}

#pragma mark - 视频头部视图跳转详情页面代理方法
- (void)HeaderViewControllerByVideoJump:(VideoSidListModel *)videoSidListModel
{
    SCHeaderViewController *hcv = [[SCHeaderViewController alloc] init];
    hcv.videoSidListModel = videoSidListModel;
    [self.navigationController pushViewController:hcv animated:YES];
    
}
#pragma mark - 视频跳转视频播放页面的代理方法
- (void)VideoPlayViewControllerJump:(VideoListModel *)videoListModel
{
    SCVideoPlayViewController *vpvc = [[SCVideoPlayViewController alloc] init];
    vpvc.videoListModel = videoListModel;
    [self.navigationController pushViewController:vpvc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
